package com.example.allplan;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Toast;

import com.example.allplan.JavaBean.BiaoQingLieBiao;
import com.example.allplan.SQhelper.SuoYouJiHuaSQhelper;
import com.example.allplan.ShuJuKuCaoZuo.NaoZhongSQCaoZuo;
import com.example.allplan.ShuJuKuCaoZuo.SuoYouChouQianSQCaoZuo;
import com.example.allplan.ShuJuKuCaoZuo.SuoYouJiHuaBaoSQCaoZuo;
import com.example.allplan.ShuJuKuCaoZuo.SuoYouJiHuaSQCaoZuo;
import com.example.allplan.StaticFangfa.BiaoQingZhuanHuan;

import java.util.ArrayList;

public class MyApplication extends Application {
    public int ji_hua_bao_shu, ji_hua_bao_xia_biao, ji_hua_tiao_shu, ji_hua_wen_jian_xia_biao,nao_zhong_xiabiao,nao_zhong_ge_shu,chou_qian_xia_biao,chou_qian_shu;
    public final String JI_HUA_BAO_SHU_SHARE_NAME = "ji_hua_bao_shu";
    public final String JI_HUA_BAO_XIA_BIAO_SHARE_NAME = "ji_hua_bao_xia_biao";
    public final String JI_HUA_TIAO_SHU_SHARE_NAME = "ji_hua_tiao_shu";
    public final String JI_HUA_WEN_JIAN_XIA_BIAO_SHARE_NAME = "ji_hua_wen_jian_xia_biao";
    public final String NAO_ZHONG_SHU_SHARE_NAME = "nao_zhong_shu";
    public final String NAO_ZHONG_XIA_BIAO_SHARE_NAME = "nao_zhong_xia_biao";
    public final String CHOU_QIAN_SHU_SHARE_NAME = "chou_qian_shu";
    public final String CHOU_QIAN_XIA_BIAO_SHARE_NAME = "chou_qian_xia_biao";
    public final String SHUANG_JI_SHARE_NAME = "shuang_ji";
    public SuoYouJiHuaBaoSQCaoZuo mSuoYouJiHuaBaoSQCaoZuo;
    public SuoYouJiHuaSQCaoZuo mSuoYouJiHuaSQCaoZuo;
    public SuoYouChouQianSQCaoZuo mSuoYouChouQianSQCaoZuo;
    public NaoZhongSQCaoZuo mNaoZhongCaoZuo;
    public SharedPreferences msharedPreferences;
    public SharedPreferences.Editor meditor;
    int biao_qing_shu_zu[] = {128513, 6, 128521, 5, 128527, 1, 128530, 3, 128534, 1, 128536, 1, 128538, 1, 128540, 3, 128544, 6, 128552, 4, 128557, 1, 128560, 4, 128565, 1, 128567, 1, 128568, 9, 128581, 7};
    public ArrayList<BiaoQingLieBiao> mBiao_Qing_int_Array;
    public boolean is_jia_zai_wan_bi,shifoushuangji;
    public static MyApplication myApplication;


    class MyThread extends Thread {
        int mlist[];
        BiaoQingShuZuCallback mcall;


        public MyThread(int[] list, BiaoQingShuZuCallback call) {
            mlist = list;
            mcall = call;
        }

        public void run() {
            ArrayList<BiaoQingLieBiao> tempintArray=new ArrayList<BiaoQingLieBiao>();
            for (int i = 0; i < 32; i += 2) {
                int temp = mlist[i];
                for (int j = 0; j < mlist[i + 1]; j++) {
                    tempintArray.add(new BiaoQingLieBiao(temp,BiaoQingZhuanHuan.biao_qing_int_to_string(temp)));
                    temp++;
                }
            }
            mcall.call(tempintArray, true);
        }
    }

    public static MyApplication getInstance() {
        return myApplication;
    }

    public interface BiaoQingShuZuCallback {
        public void call(ArrayList<BiaoQingLieBiao> mali,boolean isfinish);
    }

    public void onCreate() {
        super.onCreate();
        is_jia_zai_wan_bi = false;
        msharedPreferences = getSharedPreferences("share", Context.MODE_PRIVATE);
        myApplication = this;
        reload_biao_qing(new BiaoQingShuZuCallback() {
            @Override
            public void call(ArrayList<BiaoQingLieBiao> mali, boolean isfinish) {
                mBiao_Qing_int_Array=mali;
                is_jia_zai_wan_bi=isfinish;
                //Toast.makeText(MyApplication.this, "true", Toast.LENGTH_SHORT).show();
            }
        });
        reloadAllXml();
        reloadAllDataBase();
    }

    public void reload_biao_qing(BiaoQingShuZuCallback mcb) {
        MyThread mth = new MyThread(biao_qing_shu_zu, mcb);
        mth.start();
    }

    public void reloadAllDataBase() {
        reloadSuoYouJiHuaBaoSQCaoZuo();
        reloadJiHuaSQCaozuo();
        reloadNaoZhongSQCaozuo();
        reloadSuoYouChouQianSQCaoZuo();
    }
    public void reloadJiHuaSQCaozuo(){
        mSuoYouJiHuaSQCaoZuo=new SuoYouJiHuaSQCaoZuo(this);
    }
    public void reloadSuoYouJiHuaBaoSQCaoZuo() {
        mSuoYouJiHuaBaoSQCaoZuo = new SuoYouJiHuaBaoSQCaoZuo(this);
    }
    public void reloadSuoYouChouQianSQCaoZuo() {
        mSuoYouChouQianSQCaoZuo = new SuoYouChouQianSQCaoZuo(this);
    }

    public void reloadNaoZhongSQCaozuo(){
        mNaoZhongCaoZuo=new NaoZhongSQCaoZuo(this);
    }
    public void openXml() {
        meditor = msharedPreferences.edit();
    }

    public void commitXml() {
        meditor.commit();
    }

    public void reloadAllXml() {

        reload_ji_hua_bao_shu();
        reload_ji_hua_bao_xia_biao();
        reload_ji_hua_tiao_shu();
        reload_ji_hua_wen_jian_xia_biao();
        reload_nao_zhong_shu();
        reload_nao_zhong_xia_biao();
        reload_chou_qian_shu();
        reload_chou_qian_xia_biao();
        reload_shuang_ji();
    }
    public synchronized void putSH_Int(String name, int value) {
        openXml();
        meditor.putInt(name, value);
        commitXml();
    }
    public void reload_chou_qian_shu() {
        chou_qian_shu = msharedPreferences.getInt(CHOU_QIAN_SHU_SHARE_NAME, -1);
        if (chou_qian_shu == -1) {
            chou_qian_shu = 0;
            putSH_Int(CHOU_QIAN_SHU_SHARE_NAME, 0);
        }
    }
    public synchronized void putSH_Boolean(String name, boolean value) {
        openXml();
        meditor.putBoolean(name, value);
        commitXml();
    }
    public void reload_shuang_ji() {
        shifoushuangji = msharedPreferences.getBoolean(SHUANG_JI_SHARE_NAME, false);


           putSH_Boolean(SHUANG_JI_SHARE_NAME,shifoushuangji);

    }
    public void reload_chou_qian_xia_biao() {
        chou_qian_xia_biao= msharedPreferences.getInt(CHOU_QIAN_XIA_BIAO_SHARE_NAME, -1);
        if (chou_qian_xia_biao == -1) {
           chou_qian_xia_biao = 0;
            putSH_Int(CHOU_QIAN_XIA_BIAO_SHARE_NAME, 0);
        }
    }
    public void reload_ji_hua_bao_shu() {
        ji_hua_bao_shu = msharedPreferences.getInt(JI_HUA_BAO_SHU_SHARE_NAME, -1);
        if (ji_hua_bao_shu == -1) {
            ji_hua_bao_shu = 0;
            putSH_Int(JI_HUA_BAO_SHU_SHARE_NAME, 0);
        }
    }

    public void reload_ji_hua_bao_xia_biao() {
        ji_hua_bao_xia_biao = msharedPreferences.getInt(JI_HUA_BAO_XIA_BIAO_SHARE_NAME, -1);
        if (ji_hua_bao_xia_biao == -1) {
            ji_hua_bao_xia_biao = 2;
            putSH_Int(JI_HUA_BAO_XIA_BIAO_SHARE_NAME, 2);
        }
    }

    public void reload_ji_hua_tiao_shu() {
        ji_hua_tiao_shu = msharedPreferences.getInt(JI_HUA_TIAO_SHU_SHARE_NAME, -1);
        if (ji_hua_tiao_shu == -1) {
            ji_hua_tiao_shu = 0;
            putSH_Int(JI_HUA_TIAO_SHU_SHARE_NAME, 0);
        }
    }

    public void reload_ji_hua_wen_jian_xia_biao() {
        ji_hua_wen_jian_xia_biao = msharedPreferences.getInt(JI_HUA_WEN_JIAN_XIA_BIAO_SHARE_NAME, -1);
        if (ji_hua_wen_jian_xia_biao == -1) {
            ji_hua_wen_jian_xia_biao = 0;
            putSH_Int(JI_HUA_WEN_JIAN_XIA_BIAO_SHARE_NAME, 0);
        }
    }
    public void reload_nao_zhong_shu() {
        nao_zhong_ge_shu = msharedPreferences.getInt(NAO_ZHONG_SHU_SHARE_NAME, -1);
        if (nao_zhong_ge_shu == -1) {
            nao_zhong_ge_shu = 0;
            putSH_Int(NAO_ZHONG_SHU_SHARE_NAME, 0);
        }
    }

    public void reload_nao_zhong_xia_biao() {
        nao_zhong_xiabiao = msharedPreferences.getInt(NAO_ZHONG_XIA_BIAO_SHARE_NAME, -1);
        if (nao_zhong_xiabiao == -1) {
            nao_zhong_xiabiao = 0;
            putSH_Int(NAO_ZHONG_XIA_BIAO_SHARE_NAME, 0);
        }
    }
}
